<template>
  <el-dialog :visible.sync="dialogVisible" :title="title" width="500px" @callBack="onCallBack" @close="onClose">
    <el-form ref="ruleForm" :model="form" :rules="rules" label-width="80px" label-position="left">

      <el-form-item label="任务名称" prop="name">
        <el-input v-model="form.name" clearable placeholder="请输入任务名称"/>
      </el-form-item>

      <el-form-item label="任务时间" prop="time">
        <el-date-picker
          v-model="form.time"
          type="date"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          placeholder="选择任务时间"
        />
      </el-form-item>

      <el-form-item label="项目类型" prop="type">
        <el-select v-model="form.type" placeholder="请选择项目类型" clearable style="width: 100%;">
          <el-option
            v-for="item in types"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="优先级" prop="priority">
        <el-select v-model="form.priority" placeholder="请选择优先级" clearable style="width: 100%;">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>

    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click="onClose">取 消</el-button>
      <el-button type="primary" @click="onSubmit">确 定</el-button>
    </div>

  </el-dialog>
</template>

<script>
import { getOne, save } from '@/api/sysCalendar'

export default {
  name: 'EditCalendar',
  data() {
    return {
      dialogVisible: false,
      title: '添加',
      types: [{
        value: '1',
        label: '评标'
      }, {
        value: '2',
        label: '开标'
      }],
      options: [{
        value: '1',
        label: '一般'
      }, {
        value: '2',
        label: '重要'
      }, {
        value: '3',
        label: '非常重要'
      }],
      form: {
        id: '',
        name: '',
        time: '',
        type: '',
        priority: '1'
      },
      rules: {
        name: [
          {
            required: true,
            message: '请输入名称',
            trigger: 'blur'
          }
        ],
        time: [
          {
            required: true,
            message: '选择任务时间',
            trigger: 'blur'
          }
        ],
        type: [
          {
            required: true,
            message: '选择项目类型',
            trigger: 'blur'
          }
        ],
        priority: [
          {
            required: true,
            message: '选择优先级',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    init() {
      if (this.form.id) {
        getOne(this.form.id).then((res) => {
          if (res && res.code === 20000) {
            const rtn = res.data
            this.form.id = rtn.id
            this.form.name = rtn.name
          }
        })
      }
    },
    onShow(time) {
      this.form.time = time
      this.title = '添加日程'
      this.dialogVisible = true
    },
    onClose() {
      this.$refs['ruleForm'].resetFields()
      this.form = this.$options.data().form
      this.dialogVisible = false
    },
    onSubmit() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          save(this.form)
            .then((res) => {
              if (res && res.code === 20000) {
                this.$message({
                  message: '操作成功',
                  type: 'success'
                })
                this.dialogVisible = false
                this.onCallBack()
              }
            })
            .catch((e) => {
            })
        } else {
          return false
        }
      })
    },
    onCallBack() {
      this.$emit('onCallBack')
    }
  }
}
</script>

<style scoped>
.el-form {
  padding-left: 60px;
  padding-right: 60px;
}
</style>
